//app.js
App({
  onLaunch: function () {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 地理位置


    // 登录
    // wx.login({
    //   success: res => {
    //     // 发送 res.code 到后台换取 openId, sessionKey, unionId
        
    //   }
    // })


    
    this.userLogin();


    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },

  userLogin:function(){
    var that = this;
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        console.log(res.code);
        if (res.code) {
          // 获取用户信息
          wx.getUserInfo({
            withCredentials: true,
            success: function (res_user) {
              wx.request({
                url: "https://bag.idianwei.com/Api/Version/sendCode",
                data: {
                  code: res.code,
                  encryptedData: res_user.encryptedData,
                  iv: res_user.iv,
                },
                success: res => {
                  console.log(res.data);
                  that.globalData.userInfo = res.data;
                  wx.setStorageSync('openId', res.data.openId);
                }
              })
            },
            fail: function () {
              wx.showModal({
                title: '获取信息失败',
                content: '请允许授权以便为您提供服务',
                showCancel:false,
                success: res => {
                  if (res.confirm) {
                    wx.openSetting({
                      success: function (res) {
                        if (res.authSetting["scope.userInfo"]) { //用户点击的授权
                          wx.login({
                            success: function (res) {
                              if (res.code) {
                                wx.getUserInfo({
                                  withCredentials: true,
                                  success: function (res_user) {
                                    wx.request({
                                      url: "https://bag.idianwei.com/Api/Version/sendCode",
                                      data: {
                                        code: res.code,
                                        encryptedData: res_user.encryptedData,
                                        iv: res_user.iv,

                                      },
                                      success: res => {
                                        console.log(res.data);
                                        that.globalData.userInfo = res.data;
                                        wx.setStorageSync('openId', res.data.openId);
                                      }
                                    })
                                  }
                                })
                              }
                            }
                          })
                        }
                      }
                    })
                  }
                }

              })
            }
          })
        }
      }

    })
  },

  globalData: {
    userInfo: null,
    url:'https://bag.idianwei.com'+'/',
    userId:22,
    token: 1516268418139,
  }
})